$(function() {
    "use strict";

    /**
     * Grid System Overlay
     */
    /*
     $('div#gs-overlay').gsoverlay({
     trigger: $('a#gs-overlay-badge'),
     grid: 748,
     color: 'black',
     opacity: 0.15
     });
     */

    /**
     * Redirect all 401 ajax responses to login
     */
    $(document).ajaxError(function(event, jqXHR, settings, thrownError) {
        if (jqXHR.status === 401) {
            window.location = "/construction/Dashboard.action";
        }
    });

    /**
     * Turn off caching for all ajax requests
     */
    $.ajaxSetup({
        cache: false
    });

    /**
     * Clear form in modal when hidden
     */
    $('body').on('hidden', '.modal', function() {
        var $this = $(this);
        $this.find('form input[type=text], textarea').val('');
        $this.find('.error').removeClass('error');
        $this.find('.alert-error').remove();
        $this.find('.control-group .help-block').empty(); //might not be error message
    });

    /**
     * Submit modal form on enter
     */
    $('body').on('keypress', '.modal form input', function(e) {
        if (e.which === 13) {
            e.preventDefault();
            $(this).parents('form').submit();
        }
    });

    /**
     * Forms with data-async-target attr will submit via ajax and replace the
     * target with the response
     */
//    $('form[data-async-target]').live('submit', function() {
//        var $form = $(this);
//        var $target = $('#' + $form.attr('data-async-target'));
//
//        $.post($form.attr('action'), $form.serialize())
//            .done(function(data, status) {
//                $target.html(data);
//            })
//            .fail(function(jqXHR, textStatus, errorThrown) {
//                if (jqXHR.status === 500) {
//                    $form.prepend($('<div class="alert alert-error"><strong>Error!</strong>' + jqXHR.responseText + '</div>'));
//                }
//            });
//
//        return false;
//    });

    $('i.icon-info').tooltip();
    $('.nav-phase-list-link').tooltip({
        delay: {
            show: 800
        },
        placement: 'right'
    });

    $('.fileupload').fileupload();

});
